<?php
require('dbconnection.php');

//check email is correct format
/*
 $email = isset($_POST['email']) ? $_POST['email']: "";
 $vision = isset($_POST['vision']) ? $_POST['vision']: "";
 $satisfaction = isset($_POST['satisfaction']) ? $_POST['satisfaction']:"";
 $comments= isset($_POST['comments']) ? $_POST['comments']: '';
 $reader= isset($_POST['reader']) ? $_POST['reader']: '';
*/

if(isset($_POST['email']))
  $email = mysql_real_escape_string($_POST['email']);

if(isset($_POST['vision']))
  $vision = mysql_real_escape_string($_POST['vision']);

if(isset($_POST['satisfaction']))
  $satisfaction = mysql_real_escape_string$_POST['satisfaction']);

if(isset($_POST['comments']))
  $comments = mysql_real_escape_string($_POST['comments']);

if(isset($_POST['reader']))
  $reader = mysql_real_escape_string($_POST['reader']);





// Keeps track of the number of errors
$errorLength = 0;

// This function deals with the isset as well as formating issues
$email = htmlspecialchars($email);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
  $errorMessages[$errorLength++] ="Email set is not valid<br/>";



if (!isset($_POST['vision'])){
  $errorMessages[$errorLength++] ="You have not set the status of your vision." ;

 }

// Bug not going into here.
if(isset($_POST['vision'])){
     if(($_POST['vision'] =="blind") && (!isset($_POST['reader']))){
       $errorMessages[$errorLength++] ="For your particular vision status you must indicate a screen reader.";
     }

   }



   if (!isset($_POST['satisfaction']))
     $errorMessages[$errorLength++] = "You have not set how easy you thought the system was to use." ;

   // Check if there has been an error   
   if ($errorLength > 0){
     echo"<h2>Error...</h2>";
     echo"<p>";
     echo"There was the following problems with your form:";
     echo"</p>";
     echo"<ul>";
     for($counter=0;$counter<$errorLength;$counter++)
       echo"<li>$errorMessages[$counter]</li>";
     echo"</ul>";
     echo "</p>";
     
     // Insert the code for the form 


   }else{
     $query="insert into evaluation($email,vision,satisfaction,reader,comments) values('$email','$vision','$satisfaction','$reader','$comments')";

     // There was an error with your query


     $result = mysql_query($query);

     if (!$result)
       throw new Exception("There is an error in the connection with the server"); 


     echo"<h2>Thank You...</h2>";
     echo"<p>";
     echo"Thank you taking part in my experiment.  If you have any questions about the experiment please email: ";
     echo "<a href='mailto:kellyjh@dcs.gla.ac.uk?subject=Comments on Captcha System'>kellyjh@dcs.gla.ac.uk</a>";
     echo"</p>";
   }


   ?>
